package com.spzx.channel.service.impl;
import com.alibaba.fastjson.JSON;
import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.ItemService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.domain.Product;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.domain.SkuPrice;
import com.spzx.product.domain.SkuStockVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;


@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private RemoteProductService remoteProductService;

    @Resource
    private ThreadPoolExecutor pool;

    @Override
    public ItemVo item(Long skuId) {

        long start = System.currentTimeMillis();

        ItemVo itemVo = new ItemVo();
        //获取sku信息
        CompletableFuture<ProductSku> t_sku = CompletableFuture.supplyAsync(() -> {
            R<ProductSku> productSkuR = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
            ProductSku productSku = productSkuR.getData();
            itemVo.setProductSku(productSku);
            return productSku;
        }, pool);

        //根据商品id获取商品信息
        CompletableFuture<Void> t_product = t_sku.thenAcceptAsync(new Consumer<ProductSku>() {
            @Override
            public void accept(ProductSku productSku) {
                Long productId = productSku.getProductId();
                R<Product> productR = remoteProductService.getProduct(productId, SecurityConstants.INNER);
                Product product = productR.getData();
                //根据productId获取商品轮播图列表
                String sliderUrls = product.getSliderUrls();
                //从product获取商品规格信息
                String specValue = product.getSpecValue();
                itemVo.setProduct(product);
                itemVo.setSliderUrlList(Arrays.asList(sliderUrls.split(",")));
                itemVo.setSpecValueList(JSON.parseArray(specValue));
            }
        }, pool);

        //获取商品最新价格(不从productSku中获取，因为实际开发中的productSku信息是保存到redis中，数据可能不是最新的，mysql中才是最新的数据)
        CompletableFuture<Void> t_skuPrice = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                R<SkuPrice>  skuPriceR = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER);
                SkuPrice skuPrice = skuPriceR.getData();
                itemVo.setSkuPrice(skuPrice);
            }
        }, pool);

        //根据productId获取商品详情图片列表
        CompletableFuture<Void> t_detailsImages = t_sku.thenAcceptAsync(new Consumer<ProductSku>() {
            @Override
            public void accept(ProductSku productSku) {
                String detailsImagesUrls = remoteProductService.getDetailsimagesUrlList(productSku.getProductId(), SecurityConstants.INNER);
                itemVo.setDetailsimagesUrlList(Arrays.asList(detailsImagesUrls.split(",")));
            }
        }, pool);

        //根据skuId获取商品库存信息
        CompletableFuture<Void> t_stock = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                R<SkuStockVo> skuStockVoR = remoteProductService.getSkuStockVo(skuId, SecurityConstants.INNER);
                SkuStockVo skuStockVo = skuStockVoR.getData();
                itemVo.setSkuStockVo(skuStockVo);
            }
        }, pool);
        //根据productId获取商品规格对应商品skuId信息
        CompletableFuture<Void> t_skuSpecValueMap = t_sku.thenAcceptAsync(new Consumer<ProductSku>() {
            @Override
            public void accept(ProductSku productSku) {
                R<List<ProductSku>> productSkuListR =  remoteProductService.getSkuSpecValueMap(productSku.getProductId(), SecurityConstants.INNER);
                List<ProductSku> productSkuList = productSkuListR.getData();
                Map<String, Long> skuSpecValueMap = productSkuList.stream().collect(Collectors.toMap(ProductSku::getSkuSpec, ProductSku::getId));
                itemVo.setSkuSpecValueMap(skuSpecValueMap);
            }
        }, pool);

        CompletableFuture.allOf(t_sku, t_product, t_detailsImages, t_skuSpecValueMap, t_skuPrice, t_stock).join();

        long end = System.currentTimeMillis();
        System.out.println("耗时："+(end-start));
        return itemVo;
    }
}
